package ca.quicko.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;
import org.appfuse.model.User;

/**
 * Issue about a particular Subject relating to a certain Location
 */
@Entity
public class Issue extends BaseObject implements Comparable, Serializable {
	private static final long serialVersionUID = -6241770522813465477L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(nullable = false)
	private String title;

	@Lob
	private String description;

	@OneToMany
	private List<Solution> solution;

	@OneToMany
	private List<Act> act;

	/**
	 * Creator of this issue
	 */
	@ManyToOne(optional = false)
	private User user;

	private boolean active;

	/**
	 * Creation date
	 */
	private Date date;

	public Issue() {
		// Entity must have a no-arg public or protected constructor
	}

	public List<Act> getAct() {
		return act;
	}

	public boolean isActive() {
		return active;
	}

	public Date getDate() {
		return date;
	}

	public String getDescription() {
		return description;
	}

	public Long getId() {
		return id;
	}

	public List<Solution> getSolution() {
		return solution;
	}

	public String getTitle() {
		return title;
	}

	public User getUser() {
		return user;
	}

	public void setAct(List<Act> act) {
		this.act = act;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSolution(List<Solution> solution) {
		this.solution = solution;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Issue myClass = (Issue) object;
		return new CompareToBuilder().append(this.id, myClass.id).append(
				this.title, myClass.title).append(this.description,
				myClass.description).append(this.active, myClass.active)
				.append(this.date, myClass.date).append(this.act, myClass.act)
				.append(this.user, myClass.user).append(this.solution,
						myClass.solution).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Issue)) {
			return false;
		}
		Issue rhs = (Issue) object;
		return new EqualsBuilder().append(this.id, rhs.id).append(this.title,
				rhs.title).append(this.description, rhs.description).append(
				this.active, rhs.active).append(this.date, rhs.date).append(
				this.act, rhs.act).append(this.user, rhs.user).append(
				this.solution, rhs.solution).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-165907759, -1069995141).append(this.id)
				.append(this.title).append(this.description)
				.append(this.active).append(this.date).append(this.act).append(
						this.user).append(this.solution).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("active", this.active).append("date", this.date).append(
				"user", this.user).append("description", this.description)
				.append("title", this.title).append(
						"id", this.id).toString();
	}

}
